package com.jiuyou.storeController.mall;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.adapayBalanceRecord.domain.AdapayBalanceRecord;
import com.ruoyi.adapayBalanceRecord.service.IAdapayBalanceRecordService;
import com.ruoyi.clerkBalanceRecord.service.IClerkBalanceRecordService;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.member.service.IMemberService;
import com.ruoyi.shopping.domain.MallBind;
import com.ruoyi.shopping.service.IMallBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chengliang
 * @date 2022/8/5
 **/
@RestController
@RequestMapping("/store/withDraw")
public class MallWithDrawClientController extends StoreBaseController {

    @Autowired
    private IMallBindService bindService;
    @Autowired
    private IAdapayBalanceRecordService recordService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IClerkBalanceRecordService clerkRecordService;

    @RequestMapping("/getCustomerList")
    public AjaxResult getCustomerList(){
        StoreUserRole storeUserRole = getStoreUserRole();

        Long storeId = storeUserRole.getStoreId();
        MallBind bind = new MallBind();
        bind.setStoreId(storeId);
        startPage();
//        JSONObject jsonObject = new JSONObject();
        List<MallBind> binds = new ArrayList<>();
//        jsonObject.put("customer",binds);
//        jsonObject.put("total",0);
//        jsonObject.put("lastPage",true);
        binds = bindService.selectMallBindList(bind);
        if(binds.size()==0){
            return AjaxResult.success(getDataTable(binds));
        }
        List<Long> collect = binds.stream().map(MallBind::getMemberId).collect(Collectors.toList());
        List<Member> ms = memberService.selectMemberByIds(collect);
        binds.forEach(e->{
            ms.forEach(m->{
                if(e.getMemberId().equals(m.getId())){
                    e.setMemberName(m.getNickName());
                    e.setImgSrc(m.getHeadImg());
                }
            });
        });
//        jsonObject.put("customer",binds);
//        jsonObject.put("total",getDataTable(binds).getTotal());
//        jsonObject.put("lastPage",getDataTable(binds).isLastPage());
        return AjaxResult.success(getDataTable(binds));

    }
    @RequestMapping("/getCommissionList")
    public AjaxResult getCommissionList(){
        StoreUserRole storeUserRole = getStoreUserRole();
        Long storeId = storeUserRole.getStoreId();
        AdapayBalanceRecord adapayBalanceRecord = new AdapayBalanceRecord();
        adapayBalanceRecord.setStoreId(storeId);
//        adapayBalanceRecord.setBusType(DicConstants.BUSTYPE_BALANCE_MALL_DIV.getCode());
        adapayBalanceRecord.setBusType(DicConstants.BUSTYPE_BALANCE_DIV.getCode());
        startPage();
        List<AdapayBalanceRecord> list = recordService.selectAdapayBalanceRecordList(adapayBalanceRecord);
        TableDataInfo tableDataInfo= getDataTable(list);
        JSONArray jsonArray=JSONArray.parseArray(JSONArray.toJSONString(list));
        for (Object o : jsonArray) {
            JSONObject jso=(JSONObject)o;
            jso.put("money", Arith.numTostr(jso.getBigDecimal("money")));
            jso.put("createTime", DateUtils.dateTimeYMDHSM(jso.getDate("createTime")));
        }
        tableDataInfo.setRows(jsonArray);
        return AjaxResult.success(tableDataInfo);
    }
    @RequestMapping("/getData")
    public AjaxResult getData(){
        StoreUserRole storeUserRole = getStoreUserRole();
        Long storeId = storeUserRole.getStoreId();
        System.out.println("dd");
        System.out.println(storeId);
        BigDecimal yesterday = BigDecimal.ZERO;
        BigDecimal count = BigDecimal.ZERO;
        yesterday = recordService.selectData(storeId,new Date());
        count = recordService.selectData(storeId,null);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("yesterday",Arith.numTostr(yesterday));
        map.put("count",Arith.numTostr(count));
        return AjaxResult.success(map);
    }


    /**
     * 员工的分销收入
     * */
    @RequestMapping("/getClerkData")
    public AjaxResult getClerkData(){
//        StoreUserRole storeUserRole = getStoreUserRole();
//        Long storeId = storeUserRole.getStoreId();
        Long storeUserId = getLoginStoreUserId();

        BigDecimal yesterday = BigDecimal.ZERO;
        BigDecimal count = BigDecimal.ZERO;
        yesterday = clerkRecordService.selectData(storeUserId,new Date());
        count = clerkRecordService.selectData(storeUserId,null);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("yesterday",Arith.numTostr(yesterday));
        map.put("count",Arith.numTostr(count));
        return AjaxResult.success(map);
    }


}
